package proj.unions.resource;

import proj.unions.general.OnMyCallBack;
import proj.unions.general.SuperTools;
import proj.unions.general.Text;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;

public class CheckNetworkState implements Runnable, RMConstDefine{
	
	private Activity mActivity = null;
	private OnMyCallBack mOnMyCallBack = null;
	
	public CheckNetworkState(Activity a, OnMyCallBack l)
	{
		mActivity = a;
		mOnMyCallBack = l;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		if (mActivity == null || mOnMyCallBack == null)
		{
			mOnMyCallBack.onReceive("error", 0, null);
			return;
		}
		
		if(isNetworkAvailable())
			mOnMyCallBack.onReceive("yes", 1, null);
		else
		{
			SuperTools.getInstance().showErrorDialog(Text.getKey("Net")/*"网络"*/, Text.getKey("Net_CallManager_ErrorNet")/*"网络连接异常，请联系管理员*/);
		}
	}
	
	public static boolean isNetworkAvailable() 
	{
		Activity a = SuperTools.getInstance().getCurActivity();
		ConnectivityManager conMan = (ConnectivityManager)a.getSystemService(Context.CONNECTIVITY_SERVICE);
		State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
		NetworkInfo mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		
		if( mobile != null && (mobile.getState() == State.CONNECTED || 
				mobile.getState() == State.CONNECTING))
			  return true;
        
        if(wifi==State.CONNECTED||wifi==State.CONNECTING)
            return true;
        return false;
	}




}
